package com.concurrent.queue.demo1;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;

public class JuiceShop {

	private ArrayBlockingQueue<Juice> juicesQueue = new ArrayBlockingQueue<Juice>(10);

	private List<JuiceMachine> machines = new ArrayList<JuiceMachine>();

	public List<Juice> getJuice(int count) throws InterruptedException {
		System.out.println("购买果汁:" + count);
		List<Juice> js = new ArrayList<Juice>();
		for (int i = 0; i < count; i++) {
			js.add(juicesQueue.take());
		}
		System.out.println("交易完成, 还有库存:" + juicesQueue.size());
		return js;
	}

	public void putJuices(List<Juice> juices) throws InterruptedException {
		for (Juice juice : juices) {
			juicesQueue.put(juice);
			System.out.println("正在存货, 还有库存:" + juicesQueue.size());
		}
	}

	public void start() {
		for (int i = 0; i < 5; i++) {
			JuiceMachine machine = new JuiceMachineImpl();
			machines.add(machine);
			machine.setJuiceShop(this);
			new Thread(machine).start();
		}
	}
}
